using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Preferences;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using NorthStar.Passenger.Application;
using TheNorthStar.Api.Requests;
using TheNorthStar.Api.Results;

namespace NorthStar.Passenger
{
    public class CancelBookingAsync : AsyncTask
    {
        private readonly Context _context;
        private CancelBookingRequest _request;
        private ProgressDialog p;

        public CancelBookingAsync(Context context, CancelBookingRequest request)
        {
            _context = context;
            _request = request;
        }

        protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
        {
           
            var api = new ApiService(Helper.GetServer(_context));

            return api.CancelBooking(_request);
        }

        protected override void OnPreExecute()
        {
            base.OnPreExecute();
            p = ProgressDialog.Show(_context, "Cancelling Booking...", "Please Wait...", true, true);

        }

        protected override void OnPostExecute(Java.Lang.Object result)
        {
            base.OnPostExecute(result);
            //hide and kill the progress dialog
            p.Hide();
            p.Cancel();

            //setup to show the confirmation screen
            Intent a = new Intent(_context, typeof(HomeActivity));
            a.AddFlags(ActivityFlags.ClearTop);

            var res = Newtonsoft.Json.JsonConvert.DeserializeObject<GeneralResult>(result.ToString());
          
            if(res.Success)
            {
                a.PutExtra("source", "cancelBooking");

                //store the serialized booking for later
                ISharedPreferencesEditor editor = PreferenceManager.GetDefaultSharedPreferences(_context).Edit();
                editor.PutString("lastBookingRef", "");
                editor.Commit();
                _context.StartActivity(a);
            }
            else
            {
                Toast.MakeText(_context,"Cancel failed: " + res.Message,ToastLength.Short).Show();
            }

            

        }
    }
}